package com.antdesignbackend.backend.production.reservation.service.impl;


import com.antdesignbackend.backend.common.returnResult.ReturnVO;
import com.antdesignbackend.backend.entity.Enterprise;
import com.antdesignbackend.backend.production.reservation.entity.entityVo.ReservationVo;
import com.antdesignbackend.backend.service.EnterpriseService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.antdesignbackend.backend.production.reservation.mapper.ReservationMapper;
import com.antdesignbackend.backend.production.reservation.entity.Reservation;
import com.antdesignbackend.backend.production.reservation.service.ReservationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;


@Service
public class ReservationServiceImpl extends ServiceImpl<ReservationMapper, Reservation> implements ReservationService {
    @Autowired
    ReservationMapper reservationMapper;

    @Autowired
    ReservationService reservationService;
    @Autowired
    EnterpriseService enterpriseService;

    @Override
    public List<ReservationVo> search(QueryWrapper<ReservationVo> wrapper, Page<ReservationVo> page) {
        return reservationMapper.search(wrapper,page);
    }

    @Override
    public Integer count(QueryWrapper<ReservationVo> wrapper) {
        return reservationMapper.count(wrapper);
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public void insert(Reservation reservation) throws RuntimeException {
        //根据eid获得对应的企业信息
        Enterprise enterprise = enterpriseService.getById(reservation.getEid());
        if (enterprise == null){
            throw  new RuntimeException("企业不存在！");
        }
        //判断变动类型
        if (reservation.getOldType() == 0){ //哺乳
            enterprise.setLactation(enterprise.getLactation() - reservation.getNumber());
            if (reservation.getNewType() == 1){ //保育
                //哺乳转保育
                enterprise.setConservation(enterprise.getConservation() + reservation.getNumber());
            }else if (reservation.getNewType() == 2){ //育成
                //哺乳转育成
                enterprise.setCommodity(enterprise.getCommodity() + reservation.getNumber());
            }else if (reservation.getNewType() == 3){ //后备
                //哺乳转后备
                enterprise.setReserve(enterprise.getReserve() + reservation.getNumber());
            }
        }else if (reservation.getOldType() == 1){ //保育
            enterprise.setConservation(enterprise.getConservation() - reservation.getNumber());
            if (reservation.getNewType() == 2){ //育成
                //保育转育成
                enterprise.setCommodity(enterprise.getCommodity() + reservation.getNumber());
            }else if (reservation.getNewType() == 3){ //后备
                //保育转后备
                enterprise.setReserve(enterprise.getReserve() + reservation.getNumber());
            }
        }else if (reservation.getOldType() == 2){ //育成
            enterprise.setCommodity(enterprise.getCommodity() - reservation.getNumber());
            if (reservation.getNewType() == 3){ //后备
                //育成转后备
                enterprise.setReserve(enterprise.getReserve() + reservation.getNumber());
            }
        }
        enterpriseService.updateById(enterprise);
        reservationService.save(reservation);
    }

    @Override
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    public void update(Reservation reservation) throws RuntimeException {
        //先还原数据
        Reservation reservation1 = reservationService.getById(reservation.getId());
        Enterprise enterprise = enterpriseService.getById(reservation1.getEid());
        if (enterprise == null){
            throw  new RuntimeException("企业不存在！");
        }
        //判断变动类型
        if (reservation1.getOldType() == 0){ //哺乳
            enterprise.setLactation(enterprise.getLactation() + reservation1.getNumber());
            if (reservation1.getNewType() == 1){ //保育
                //哺乳转保育
                enterprise.setConservation(enterprise.getConservation() - reservation1.getNumber());
            }else if (reservation1.getNewType() == 2){ //育成
                //哺乳转育成
                enterprise.setCommodity(enterprise.getCommodity() - reservation1.getNumber());
            }else if (reservation1.getNewType() == 3){ //后备
                //哺乳转后备
                enterprise.setReserve(enterprise.getReserve() - reservation1.getNumber());
            }
        }else if (reservation1.getOldType() == 1){ //保育
            enterprise.setConservation(enterprise.getConservation() + reservation1.getNumber());
            if (reservation1.getNewType() == 2){ //育成
                //保育转育成
                enterprise.setCommodity(enterprise.getCommodity() - reservation1.getNumber());
            }else if (reservation1.getNewType() == 3){ //后备
                //保育转后备
                enterprise.setReserve(enterprise.getReserve() - reservation1.getNumber());
            }
        }else if (reservation1.getOldType() == 2){ //育成
            enterprise.setCommodity(enterprise.getCommodity() + reservation1.getNumber());
            if (reservation1.getNewType() == 3){ //后备
                //育成转后备
                enterprise.setReserve(enterprise.getReserve() - reservation1.getNumber());
            }
        }
        //判断数量是否足够
        if (reservation.getOldType() == 0){ //哺乳
            if (enterprise.getLactation() < reservation.getNumber()){
                throw new RuntimeException("变动数量超出实际数量,保存失败！");
            }
        }else if (reservation.getOldType() == 1){ //保育
            if (enterprise.getConservation() < reservation.getNumber()){
                throw new RuntimeException("变动数量超出实际数量,保存失败！");
            }
        }else if (reservation.getOldType() == 2){ //育成
            if (enterprise.getCommodity() < reservation.getNumber()){
                throw new RuntimeException("变动数量超出实际数量,保存失败！");
            }
        }
        //判断变动类型
        if (reservation.getOldType() == 0){ //哺乳
            enterprise.setLactation(enterprise.getLactation() - reservation.getNumber());
            if (reservation.getNewType() == 1){ //保育
                //哺乳转保育
                enterprise.setConservation(enterprise.getConservation() + reservation.getNumber());
            }else if (reservation.getNewType() == 2){ //育成
                //哺乳转育成
                enterprise.setCommodity(enterprise.getCommodity() + reservation.getNumber());
            }else if (reservation.getNewType() == 3){ //后备
                //哺乳转后备
                enterprise.setReserve(enterprise.getReserve() + reservation.getNumber());
            }
        }else if (reservation.getOldType() == 1){ //保育
            enterprise.setConservation(enterprise.getConservation() - reservation.getNumber());
            if (reservation.getNewType() == 2){ //育成
                //保育转育成
                enterprise.setCommodity(enterprise.getCommodity() + reservation.getNumber());
            }else if (reservation.getNewType() == 3){ //后备
                //保育转后备
                enterprise.setReserve(enterprise.getReserve() + reservation.getNumber());
            }
        }else if (reservation.getOldType() == 2){ //育成
            enterprise.setCommodity(enterprise.getCommodity() - reservation.getNumber());
            if (reservation.getNewType() == 3){ //后备
                //育成转后备
                enterprise.setReserve(enterprise.getReserve() + reservation.getNumber());
            }
        }
        enterpriseService.updateById(enterprise);
        reservationService.updateById(reservation);
    }
}
